package clientmain;

import java.rmi.RemoteException;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;


public class DynamicServiceCallJava {

	/**
	 * 
	 * @param endpoint 服务地址(WSDL)
	 * @param method 方法名
	 * @param object 参数
	 * @param list 参数名
	 * @param targetNamespace 命名空间
	 * @param serName 服务名
	 * @param pName 端口名
	 * @param hasSoapAction 是否有soapAction
	 * @throws RemoteException
	 * @throws SOAPException
	 */
	public static void doOperate(String endpoint, String method, Object[] parameters,
			String[] parameterName, String targetNamespace, String serName, String pName,boolean hasSoapAction) throws RemoteException, SOAPException{
		QName serviceName = new QName(targetNamespace, serName);
		QName portName = new QName(targetNamespace, pName);
		Service service = Service.create(serviceName);
		service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpoint);
		Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
		BindingProvider bp = (BindingProvider) dispatch;
		Map<String, Object> rc = bp.getRequestContext();
		rc.put(BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
		
		if (hasSoapAction) {
			rc.put(BindingProvider.SOAPACTION_URI_PROPERTY, method);
		}
		
		rc.put(BindingProvider.USERNAME_PROPERTY, "lxjk");
		rc.put(BindingProvider.PASSWORD_PROPERTY, "lxjk");
		
		MessageFactory factory = ((SOAPBinding) bp.getBinding()).getMessageFactory();
		SOAPMessage request = factory.createMessage();
		SOAPBody body = request.getSOAPBody();
		QName payloadName = new QName(targetNamespace, method, "ns1");
		SOAPBodyElement payload = body.addBodyElement(payloadName);
		
		for (int i = 0; i < parameters.length; i++) {
			payload.addChildElement(parameterName[i]).addTextNode(parameters[i]+"");
		}


		SOAPMessage reply = null;
		reply = dispatch.invoke(request);
		SOAPBody soapBody = reply.getSOAPBody();
		SOAPBodyElement nextSoapBodyElement = (SOAPBodyElement) soapBody.getChildElements().next();
		SOAPElement soapElement = (SOAPElement) nextSoapBodyElement.getChildElements().next();
		System.out.println("the result is :" + soapElement.getValue());
	}
	
	public static void main(String[] args) throws RemoteException, SOAPException {
		// 名字空间
		   String targetNamespace = "http://service.authentication.petrochina.com/";
		// 服务名
		   String serName = "IAuthenticImplService";
		// 端口名
		   String pName = "IAuthenticImplPort";
		// 服务地址
		   String endpoint = "http://10.69.1.40:8080/lhauthenticservice/IAuthenticImplPort?wsdl";
		// 方法名
		   String method = "isOnlineUser";
		   
		   Object[] parameters = new Object[]{"yan-liu","10.71.58.62"};
		   
		   String[] parameterName = new String[]{"arg0","arg1"};
		   
		   doOperate(endpoint, method, parameters, parameterName, targetNamespace, serName, pName, false);
	}
}
